from django.shortcuts import render
from django.views.generic import View
from .forms import MessageForm
from django.http import HttpResponse

# Create your views here.

class IndexView(View):
    def get(self,request):
        form = MessageForm()
        #这里进行实例化 为了在页面上渲染表单
        return render(request,'index.html',context={"form":form})

    def post(self,request):
        form = MessageForm(request.POST)
        #实例化表单 用来表单验证及接收数据
        if form.is_valid():
            title = form.cleaned_data.get('title')
            content = form.cleaned_data.get('content')
            email = form.cleaned_data.get('email')
            reply = form.cleaned_data.get('reply')
            print("="*100)
            print(title)
            print(content)
            print(email)
            print(reply)
            print("="*100)
            return HttpResponse("success")
        else:
            print(form.errors.get_json_data())
            return HttpResponse("FAIL")
